package jcer.test;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
 

import com.mchange.v2.c3p0.C3P0ProxyConnection;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3p0 {

	/**
	 * @param args
	 * @throws PropertyVetoException
	 * @throws SQLException
	 * @throws InterruptedException
	 */
	public static void main(String[] args) throws PropertyVetoException,
			SQLException, InterruptedException {
		ComboPooledDataSource cpds = new ComboPooledDataSource();
		cpds.setDriverClass("com.mysql.jdbc.Driver"); // loads the jdbc driver
		cpds.setJdbcUrl("jdbc:mysql://123.125.104.141:8306/?autoReconnect=true");
		cpds.setUser("quanwei");
		cpds.setPassword("111111"); 

		// the settings below are optional -- c3p0 can work with defaults
		cpds.setMinPoolSize(5); 
		cpds.setAcquireIncrement(1);
		cpds.setMaxPoolSize(10);

		Connection conn;
		conn = (C3P0ProxyConnection) cpds.getConnection();
		conn.close();
		System.out.println("num_connections: "
				+ cpds.getNumConnectionsDefaultUser());
		System.out.println("num_busy_connections: "
				+ cpds.getNumBusyConnectionsDefaultUser());
		System.out.println("num_idle_connections: "
				+ cpds.getNumIdleConnectionsDefaultUser());


		conn = (C3P0ProxyConnection) cpds.getConnection();
		conn.close();
		System.out.println("num_connections: "
				+ cpds.getNumConnectionsDefaultUser());
		System.out.println("num_busy_connections: "
				+ cpds.getNumBusyConnectionsDefaultUser());
		System.out.println("num_idle_connections: "
				+ cpds.getNumIdleConnectionsDefaultUser());


		conn = (C3P0ProxyConnection) cpds.getConnection();
		conn.close();
		System.out.println("num_connections: "
				+ cpds.getNumConnectionsDefaultUser());
		System.out.println("num_busy_connections: "
				+ cpds.getNumBusyConnectionsDefaultUser());
		System.out.println("num_idle_connections: "
				+ cpds.getNumIdleConnectionsDefaultUser());

		conn = (C3P0ProxyConnection) cpds.getConnection();
		conn.close();
		System.out.println("num_connections: "
				+ cpds.getNumConnectionsDefaultUser());
		System.out.println("num_busy_connections: "
				+ cpds.getNumBusyConnectionsDefaultUser());
		System.out.println("num_idle_connections: "
				+ cpds.getNumIdleConnectionsDefaultUser());


		conn = (C3P0ProxyConnection) cpds.getConnection();
		conn.close();
		System.out.println("num_connections: "
				+ cpds.getNumConnectionsDefaultUser());
		System.out.println("num_busy_connections: "
				+ cpds.getNumBusyConnectionsDefaultUser());
		System.out.println("num_idle_connections: "
				+ cpds.getNumIdleConnectionsDefaultUser());

		while (true) {
			conn = (C3P0ProxyConnection) cpds.getConnection();
			ResultSet rs = conn.createStatement().executeQuery("select User,Host from mysql.user limit 2");
			System.out.println(rs.next());
			rs.close();
			conn.close();
			Thread.sleep(1000);
		}
	}

}
